package com.my.controller;

import com.google.gson.Gson;
import com.my.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/goods")
public class GoodsController {

    GoodsService goodsService;

    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @RequestMapping("/add")
    public String addGoods(String name, Double price, String imgUrl, Integer stock, String info, HttpServletRequest req) {
        return this.goodsService.addGoods(name, price, imgUrl, stock, info, req);
    }

    @RequestMapping("/delete")
    public String deleteGoods(Integer goodsId) {
        return this.goodsService.deleteGoods(goodsId);
    }

    @RequestMapping("/deleteGoods")
    public String deleteGoods(String goodsIds) {
        return this.goodsService.deleteGoods(goodsIds);
    }

    @RequestMapping("/reverse")
    public String reverseGoods(Integer goodsId) {
        return this.goodsService.reverseGoods(goodsId);
    }

    @RequestMapping("/update")
    public String updateGoods(Integer id, String name, Double price, String imgUrl, Integer stock, String info, HttpServletRequest req) {
        return this.goodsService.updateGoods(id, name, price, imgUrl, stock, info, req);
    }


    @RequestMapping("/show")
    public String showGoods(String context, Integer currentPage, Integer pageSize, String sort, Boolean asc, HttpServletRequest req) {
        System.out.println(sort);
        return this.goodsService.showGoods(context, currentPage, pageSize, sort, asc, req);
    }

    @RequestMapping("/showForUser")
    public String showGoodsForUser(String context, Integer currentPage, Integer pageSize, String sort, Boolean asc) {
        return this.goodsService.showGoodsForUser(context, currentPage, pageSize, sort, asc);
    }

    @RequestMapping("/buy")
    public String buy(Integer num, Integer goodsId, HttpServletRequest req) {
        return this.goodsService.buyGoods(num, goodsId, req);
    }

    @RequestMapping("/selById")
    public String selById(Integer id) {
        return this.goodsService.selById(id);
    }

}
